<?php

$rootdir = '../';
include_once('session.php');
include_once('tshirts.php');

function GetLignesTablePanierHTML()
{
	if(!isset($_SESSION['panier']))
		return '';
	
	$Items = $_SESSION['panier'];
	$HTML = '';
	$NumItem = 0;
	foreach($Items as $Item)
	{
		$HTML .= '	<div id="panier_ligneitemrecaptable">'.
					GetLigneTablePanierHTML($Item, $NumItem) .
				'	</div>
					<div class="panier_souligne" style="margin-top: 20px;"></div>';
		$NumItem++;
	}

	return $HTML;
}

function GetLigneTablePanierHTML($Item, $NumItem)
{
	if(!isset($Item['idtshirt']) || !isset($Item['nombre']) || !isset($Item['taille']))
		return '<p style="color: red;">Erreur interne, veuillez signaler ce problème à l\'administrateur.</p>';

	$tshirt = GetTshirtByID($Item['idtshirt']);
	if($tshirt == false)
		return '<p style="color: red;">Tshirt introuvable, veuillez signaler ce problème à l\'administrateur.</p>';

	$HTML = '
			<table class="tableitem">
				<tr >
					<td style="width: 60px;" class="tableitem_col1" style="vertical-align: top;">
						<div id="panier_boutonsupprimeritem" onclick="DeleteItemPanierByIndex(' . $NumItem . ');"></div>
					</td>
					<td class="tableitem_col2" >
						<div class="panier_divimage">
							<img src="tshirts/' . $Item['idtshirt'] . '_galery.png"/>
						</div>
					</td>			
					<td class="tableitem_col3" >
						<table style="margin-top: 20px; width: 100%;">
							<tr style="font-size: 90%;">
								<td style="width: 160px;">' . $Item['taille'] . '</td>
								<td style="width: 150px;">' . $Item['nombre'] . '</td>
								<td>' . $tshirt['prix']*$Item['nombre'] . ' Euros</td>
							</tr>
						</table>
						<div class="panier_etape1_souligne"></div>
					</td>
				</tr>
			</table>';
	
	return $HTML;
}

function AjouterTshirtPanier($idtshirt, $nombre, $taille)
{
	$NewItem = array();
	
	$NewItem['idtshirt'] = $idtshirt;
	$NewItem['nombre'] = $nombre;
	$NewItem['taille'] = $taille;

	$_SESSION['panier'][] = $NewItem;
}

function SupprimerTshirtPanierByIndex($index)
{
	if(isset($_SESSION['panier']))
	{
		if(isset($_SESSION['panier'][$index]))
		{
			unset($_SESSION['panier'][$index]);
			$_SESSION['panier'] = array_values($_SESSION['panier']);
			return true;
		}
		else
			return false;
	}
	else 
		return false;
}

function ViderPanier()
{
	if(isset($_SESSION['panier']))
		unset($_SESSION['panier']);
}

function GetNumberItemsPanier()
{
	if(!isset($_SESSION['panier']))
		return 0;
	else
		return count($_SESSION['panier']);
}

function GetNumberTshirtsPanier()
{
	if(!isset($_SESSION['panier']))
		return 0;
	else
	{
		$count = 0;
		foreach($_SESSION['panier'] as $item)
			$count += $item['nombre'];
			
		return $count;
	}
}

function GetOrderString()
{
	$String = '';

	if(isset($_SESSION['panier']))
	{
		foreach($_SESSION['panier'] as $item)
			$String .= $item['idtshirt'] . ',' . $item['nombre'] . ',' . $item['taille'] . '-';
		
		return $String;
	}
	else
		return NULL;
}

function GetTotalPricePanier()
{
	if(isset($_SESSION['panier']))
	{
		$Total = 0;
	
		foreach($_SESSION['panier'] as $item)
		{
			$tshirt = GetTshirtByID($item['idtshirt']);
			if($tshirt != NULL)
				$Total += $tshirt['prix']*$item['nombre'];
		}
		
		return $Total;
	}
	else
		return 0;
}

?>